<script>
    class Promise{
        PromiseState = 'padding'
        PromiseResult = undefined
        constructor(callback){
            // 处理报错
            try{
                callback(res => {
                    if (!this.PromiseState === 'padding') return
                    this.PromiseResult = res
                    this.PromiseState = 'success'
                }, err => {
                    if (!this.PromiseState === 'padding') return
                    this.PromiseResult = err
                    this.PromiseState = 'defeat'
                })
            }catch(err){
                console.log(err)
            }
             
        }
    }
    let p = new Promise((resolve,reject)=>{
        resolve(11)
    })
    Promise.prototype.then = function(successfn,defeatfn){
        let result = null
        if(this.PromiseState === "success"){
            result = successfn(this.PromiseResult)
        }
        if(this.PromiseState === "defeat"){
            result = defeatfn(this.PromiseResult)
        }
        return new Promise( (resolve,reject)=>{
            // 判断返回reject才会用reject 否则都是resolve
            if(result instanceof Promise){
                result.then(res=>resolve(res),err=>reject(err))
            }else{
                resolve(result)
            }
        } )
    }
</script>